package com.ali.music.navigator.backstack;

import android.os.Bundle;

/**
 * @author xinpeng.xiao
 * @version 1.0.0
 *          Created on 2016/1/18.
 */
public class BundleParam implements Params {
    private Bundle mBundle;

    private BundleParam(Bundle bundle) {
        mBundle = bundle;
    }

    public static BundleParam from(Bundle bundle) {
        assert bundle != null;
        return new BundleParam(bundle);
    }

    public int getInt(String key, int defaultValue) {
        try {
            return Integer.parseInt(mBundle.getString(key));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return mBundle.getInt(key, defaultValue);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }

    public long getLong(String key, long defaultValue) {
        try {
            return Long.parseLong(mBundle.getString(key));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return mBundle.getLong(key, defaultValue);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }


    public float getFloat(String key, float defaultValue) {
        try {
            return Float.parseFloat(mBundle.getString(key));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return mBundle.getFloat(key, defaultValue);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }


    public double getDouble(String key, double defaultValue) {
        try {
            return Double.parseDouble(mBundle.getString(key));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return mBundle.getDouble(key, defaultValue);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        try {
            return Boolean.parseBoolean(mBundle.getString(key));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return mBundle.getBoolean(key, defaultValue);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }

    @Override
    public String getString(String key, String defaultValue) {
        return mBundle.getString(key);
    }
}
